package com.czl.module_storehouse.mvp.model;

import android.util.Log;

import com.czl.base.BuildConfig;
import com.czl.base.data.bean.tengyun.BorrowBean;
import com.czl.base.data.bean.tengyun.ReceiveBean;
import com.czl.base.data.bean.tengyun.RecordRemandBean;
import com.czl.base.data.net.RetrofitClient;
import com.czl.base.util.SpHelper;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_service.system.Constants;
import com.czl.module_storehouse.api.ApiService;
import com.czl.module_storehouse.bean.BorrowReturnBean;
import com.czl.module_storehouse.bean.ReceiveReturnNeedBean;
import com.czl.module_storehouse.bean.RemandReturnBean;
import com.google.gson.Gson;

import org.json.JSONObject;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @author by xubin,
 * @date on 2022/2/14.
 * PS: Not easy to write code, please indicate.
 */
public class RemandHomeModel extends BasicDataModel {

    public Observable<HttpResponse<Object>> receiveReturn(ReceiveReturnNeedBean bean) {
        bean.setOperaterId(getUserId());
        bean.setOperaterName(getTName());
        Gson gson = new Gson();
        String json = gson.toJson(bean);
        if (BuildConfig.DEBUG) {
            Log.e(TAG, "ReceiveReturnNeedBean :" + json);
        }
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class)
                .receiveReturn(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<Object>> borrowReturn(BorrowReturnBean bean) {
        bean.setOperaterId(getUserId());
        bean.setOperaterName(getTName());
        Gson gson = new Gson();
        String json = gson.toJson(bean);
        if (BuildConfig.DEBUG) {
            Log.e(TAG, "BorrowReturnBean :" + json);
        }
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class)
                .borrowReturn(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<Object>> directReturn(RemandReturnBean bean) {
        bean.setOperaterId(getUserId());
        bean.setOperaterName(getTName());
        bean.setStorehouseId(getStorehouseId());
        Gson gson = new Gson();
        String json = gson.toJson(bean);
        if (BuildConfig.DEBUG) {
            Log.e(TAG, "RemandReturnBean :" + json);
        }
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class)
                .directReturn(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<ListBean<RecordRemandBean>>> getSortUseInfo(String userName, Integer sortId, int pageNo) {
        JSONObject object = new JSONObject();
        try {
            object.put("storehouseId", getStorehouseId());
            object.put("userName", userName);
            object.put("pageNo", pageNo);
//            object.put("pageSize", 10);
            object.put("sortId", sortId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json = object.toString();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class).getSortUseInfo(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<ListBean<RecordRemandBean>>> getProUseInfo(String userName, String sortName) {
        JSONObject object = new JSONObject();
        try {
            object.put("sortName", sortName);
            object.put("userName", userName);
            object.put("storehouseId", getStorehouseId());
            object.put("isPasteCode", 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json = object.toString();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class)
                .getProUseInfo(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<ListBean<RecordRemandBean>>> getProUseInfo(String searchName, int pageNo) {
        JSONObject object = new JSONObject();
        try {
            object.put("searchName", searchName);
            object.put("pageNo", pageNo);
            object.put("storehouseId", getStorehouseId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json = object.toString();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class)
                .getProUseInfo(RequestBody.create(mediaType, json));
    }


    public Observable<HttpResponse<ListBean<RecordRemandBean>>> getDamageSortList(String searchName, int pageNo) {
        JSONObject object = new JSONObject();
        try {
            object.put("searchName", searchName);
            object.put("pageNo", pageNo);
            object.put("storehouseId", getStorehouseId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json = object.toString();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class)
                .getDamageSortList(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<ListBean<ReceiveBean>>> getNeedDealtReceiveList() {
        JSONObject object = new JSONObject();
        try {
            object.put("companyId", SpHelper.INSTANCE.decodeInt(Constants.SpKey.MANAGE_COMPANY_ID));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json = object.toString();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class)
                .getNeedDealtReceiveList(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<ListBean<ReceiveBean>>> getDealtReceiveList(int pageNo) {
        JSONObject object = new JSONObject();
        try {
            object.put("storehouseId", getStorehouseId());
            object.put("pageNo", pageNo);
            object.put("pageSize", 10);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json = object.toString();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class)
                .getNeedDealtReceiveList(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<ListBean<ReceiveBean>>> getNeedReturnReceiveList(int pageNo) {
        JSONObject object = new JSONObject();
        try {
            object.put("storehouseId", getStorehouseId());
            object.put("pageNo", pageNo);
            object.put("pageSize", 10);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json = object.toString();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class)
                .getNeedReturnReceiveList(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<ListBean<ReceiveBean>>> getReturnReceiveList(int pageNo) {
        JSONObject object = new JSONObject();
        try {
            object.put("storehouseId", getStorehouseId());
            object.put("pageNo", pageNo);
            object.put("pageSize", 10);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json = object.toString();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class)
                .getReturnReceiveList(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<ListBean<BorrowBean>>> getNeedReturnBorrowList(int pageNo) {
        JSONObject object = new JSONObject();
        try {
            object.put("storehouseId", getStorehouseId());
            object.put("pageNo", pageNo);
            object.put("pageSize", 10);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json = object.toString();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class)
                .getNeedReturnBorrowList(RequestBody.create(mediaType, json));
    }

    public Observable<HttpResponse<ListBean<BorrowBean>>> getReturnBorrowList(int pageNo) {
        JSONObject object = new JSONObject();
        try {
            object.put("storehouseId", getStorehouseId());
            object.put("pageNo", pageNo);
            object.put("pageSize", 10);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String json = object.toString();
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        return RetrofitClient.getInstance().create(ApiService.class)
                .getReturnBorrowList(RequestBody.create(mediaType, json));
    }
}
